package cn.zxcoder.controller.login;


import cn.zxcoder.entity.AccountRole;
import cn.zxcoder.entity.login.Account;
import cn.zxcoder.entity.Res;
import cn.zxcoder.service.IAccountService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Api(tags = "注册账号")
@RestController
@RequestMapping("/api")
public class RegisterController {

    @Autowired
    IAccountService accountServiceIml;

    @PostMapping("/register")
    public Res register(@RequestParam String username, @RequestParam String password, @RequestParam String name,
                        @RequestParam String phone, @RequestParam String sex,  @RequestParam String mail) {
        if (!username.equals("") && !password.equals("")) {
            Account account = new Account(username, password);
            account.setMail(mail);
            account.setName(name);
            account.setSex(sex);
            account.setPhone(phone);
            account.setStatus("0");
            account.setEditTime(new Date());

            boolean save = accountServiceIml.save(account);

            //创建对应的用户角色表
            AccountRole accountRole = new AccountRole();
            accountRole.setAccountId(account.getId());
            if (save) {
                return new Res(save, null, "注册账户成功");
            } else {
                return new Res(save, null, "注册账户失败");
            }
        }
        return new Res(false, null, "注册账户失败");
    }

}
